package com.wm.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Collect;
import com.wm.bean.OrderResMenu;
import com.wm.bean.Orders;
import com.wm.bean.Restaurant;
import com.wm.bean.User;
import com.wm.comm.Constant;
import com.wm.comm.MD5;
import com.wm.service.CollectService;
import com.wm.service.OrderResMenuService;
import com.wm.service.OrdersService;
import com.wm.service.RestaurantService;
import com.wm.service.UserService;
import com.wm.service.web.PageList;
import com.wm.service.web.Pages;
import com.wm.web.servlet.AdminSession;
import com.wm.web.servlet.UserSession;

public class OrdersAction extends BaseAction implements SessionAware {
	private static final Log logger = LogFactory.getLog(OrdersAction.class);
	
	private OrdersService ordersService;
	private OrderResMenuService orderResMenuService;

	public OrderResMenuService getOrderResMenuService() {
		return orderResMenuService;
	}

	public void setOrderResMenuService(OrderResMenuService orderResMenuService) {
		this.orderResMenuService = orderResMenuService;
	}

	public OrdersService getOrdersService() {
		return ordersService;
	}

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}

	private Map session;

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}
	
	private String result;
	private long id;
	private Orders orders;
	private String leftType;	//左侧导航类型
	private String returnURL;

	public String getReturnURL() {
		return returnURL;
	}

	public void setReturnURL(String returnURL) {
		this.returnURL = returnURL;
	}

	public String getLeftType() {
		return leftType;
	}

	public void setLeftType(String leftType) {
		this.leftType = leftType;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	public Orders getOrders() {
		return orders;
	}

	public void setOrders(Orders orders) {
		this.orders = orders;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	private PageList pageList;
	
	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}	
	
	public String execute() {
		try {
			if(this.getSession().get(Constant.USER_SESSION_KEY)!=null){
				return this.executeMethod(this.getAction());
			}
			return "default";
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 30天内的记录
	 */
	public String index(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/orders.action");
		this.setPageList(this.getOrdersService().findOrdersByUserIdFindType(us.getUid(),30,"addTime", 1, pages));
		this.setLeftType("2");
		return "list";
	}
	
	/**
	 * 查询30天之前的
	 * @return
	 */
	public String all(){
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		Pages pages = new Pages();
		pages.setPage(this.getPage());
		pages.setPerPageNum(Constant.PAGE_NUM);
		if (this.getTotal() > 0) {
			pages.setTotalNum(this.getTotal());
		}
		pages.setFileName("/orders.action");
		this.setPageList(this.getOrdersService().findOrdersByUserIdFindType(us.getUid(),365,"addTime", 1, pages));
		this.setLeftType("3");
		return "list";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		if(this.getId()>0){
			this.getOrdersService().removeOrdersById(this.getId());
		}
		this.setAction("index");
		return "indexAction";
	}
	
	/**
	 * 查询指定订单状态
	 * @return
	 */
	public String select(){
		if(this.getId()>0){
			UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
			Orders o = this.getOrdersService().findOrdersByid(this.getId());
			if(o.getUserId() == us.getUid()){
				this.setOrders(o);
			}
		}
		return "orders";
	}
	
	/**
	 * 添加订单
	 */
	public String save(){
		this.setOrders((Orders)this.getSession().get(Constant.SHOPPING_SESSION_KEY));
		UserSession us = (UserSession)this.getSession().get(Constant.USER_SESSION_KEY);
		if(this.getOrders()!=null && this.getOrders().getMenuList()!=null){
			this.getOrders().setLinkMan(us.getUsername());
			this.getOrders().setPhone("");
			this.getOrders().setAddress("");
			this.getOrders().setState(0);
			this.getOrders().setUserId(us.getUid());
			Orders o= this.getOrdersService().saveOrders(this.getOrders());
			for(int i=0;i<this.getOrders().getMenuList().size();i++){
				OrderResMenu orderResMenu = (OrderResMenu)this.getOrders().getMenuList().get(i);
				orderResMenu.setOrderId(o.getId());
				this.getOrderResMenuService().saveOrderResMenu(orderResMenu);
			}
		}
		//清空购物车
		this.getSession().remove(Constant.SHOPPING_SESSION_KEY);
		return "json";
	}
	
	/**
	 * 修改订单
	 */
	public String update(){
		
		return "";
	}
}
